
package com.wdf.dota;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.wdf.bean.Dota2EquipmentDialog;
import com.wdf.bean.DotaEquipment;

public class Dota2EquipmentActivity extends Activity implements OnCheckedChangeListener{
	
	private RadioGroup mRadioGroup;
	private RadioButton mRadioButton1;
	private RadioButton mRadioButton2;
	private RadioButton mRadioButton3;



	private float mCurrentCheckedRadioLeft;//当前被选中的RadioButton距离左侧的距离
	private HorizontalScrollView mHorizontalScrollView;//上面的水平滚动控件
	private ViewPager mViewPager;	//下方的可横向拖动的控件
	private ArrayList<View> mViews;//用来存放下方滚动的layout(layout_1,layout_2,layout_3)
	
	private Bundle bundle;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 setContentView(R.layout.dota2_equi_menu);
	        
	        iniController();
	        iniListener();
	        iniVariable();
	        
	        mRadioButton1.setChecked(true);
	        mViewPager.setCurrentItem(1);
	        mCurrentCheckedRadioLeft = getCurrentCheckedRadioLeft();
	        
	        initEquipment();

	    
	}
	
	private void initEquipment(){
		String[] basic = DotaEquipment.dota2Equipment_basic;
		for(String equi : basic){
			int resID = getResources().getIdentifier(equi, "id", "com.wdf.dota"); 
			ImageView view = (ImageView)mViews.get(1).findViewById(resID);
   		    InputStream fis;
			try {
				fis = getAssets().open( "dota2/"+equi +".jpg");
				Bitmap bm = BitmapFactory.decodeStream(fis);
				view.setImageBitmap(bm);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//合成
				String[] combining = DotaEquipment.dota2Equipment_combining;
				for (String equi : combining) {
					int resID = getResources()
							.getIdentifier(equi, "id", "com.wdf.dota");
					ImageView view = (ImageView) mViews.get(2).findViewById(resID);
					InputStream fis;
					try {
						fis = getAssets().open("dota2/" + equi + ".jpg");
						Bitmap bm = BitmapFactory.decodeStream(fis);
						view.setImageBitmap(bm);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				
				//神秘
				String[] mystical = DotaEquipment.dota2Equipment_mystical;
				for (String equi : mystical) {
					int resID = getResources()
							.getIdentifier(equi, "id", "com.wdf.dota");
					ImageView view = (ImageView) mViews.get(3).findViewById(resID);
					InputStream fis;
					try {
						fis = getAssets().open("dota2/" + equi + ".jpg");
						Bitmap bm = BitmapFactory.decodeStream(fis);
						view.setImageBitmap(bm);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
		
	}
	
	  private void iniVariable() {
			// TODO Auto-generated method stub
	    	mViews = new ArrayList<View>();
	    	mViews.add(getLayoutInflater().inflate(R.layout.dota2_equi_menu0, null));
	    	mViews.add(getLayoutInflater().inflate(R.layout.dota2_equi_menu1, null));
	    	mViews.add(getLayoutInflater().inflate(R.layout.dota2_equi_menu2, null));
	    	mViews.add(getLayoutInflater().inflate(R.layout.dota2_equi_menu3, null));
	    
	    	
	    	mViewPager.setAdapter(new MyPagerAdapter());//设置ViewPager的适配器
		}
	    
	    /**
		 * RadioGroup点击CheckedChanged监听
		 */
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			
			AnimationSet _AnimationSet = new AnimationSet(true);
			TranslateAnimation _TranslateAnimation;
			

			if (checkedId == R.id.btn1) {
				_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, getResources().getDimension(R.dimen.rdo1), 0f, 0f);
				_AnimationSet.addAnimation(_TranslateAnimation);
				_AnimationSet.setFillBefore(false);
				_AnimationSet.setFillAfter(true);
				_AnimationSet.setDuration(100);

				mViewPager.setCurrentItem(1);//让下方ViewPager跟随上面的HorizontalScrollView切换
			}else if (checkedId == R.id.btn2) {
				_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, getResources().getDimension(R.dimen.rdo2), 0f, 0f);

				_AnimationSet.addAnimation(_TranslateAnimation);
				_AnimationSet.setFillBefore(false);
				_AnimationSet.setFillAfter(true);
				_AnimationSet.setDuration(100);

			
		
				
				mViewPager.setCurrentItem(2);
			}else if (checkedId == R.id.btn3) {
				_TranslateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, getResources().getDimension(R.dimen.rdo3), 0f, 0f);
				
				_AnimationSet.addAnimation(_TranslateAnimation);
				_AnimationSet.setFillBefore(false);
				_AnimationSet.setFillAfter(true);
				_AnimationSet.setDuration(100);
				
			
		
				
				mViewPager.setCurrentItem(3);
			}
			
			mCurrentCheckedRadioLeft = getCurrentCheckedRadioLeft();//更新当前蓝色横条距离左边的距离
			
			Log.i("zj", "getCurrentCheckedRadioLeft="+getCurrentCheckedRadioLeft());
			Log.i("zj", "getDimension="+getResources().getDimension(R.dimen.rdo2));
			
			mHorizontalScrollView.smoothScrollTo((int)mCurrentCheckedRadioLeft-(int)getResources().getDimension(R.dimen.rdo2), 0);
		}
	    
		/**
	     * 获得当前被选中的RadioButton距离左侧的距离
	     */
		private float getCurrentCheckedRadioLeft() {
			// TODO Auto-generated method stub
			if (mRadioButton1.isChecked()) {
				//Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo1));
				return getResources().getDimension(R.dimen.rdo1);
			}else if (mRadioButton2.isChecked()) {
				//Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo2));
				return getResources().getDimension(R.dimen.rdo2);
			}else if (mRadioButton3.isChecked()) {
				//Log.i("zj", "currentCheckedRadioLeft="+getResources().getDimension(R.dimen.rdo3));
				return getResources().getDimension(R.dimen.rdo3);
			}
			return 0f;
		}

		private void iniListener() {
			// TODO Auto-generated method stub
			
			mRadioGroup.setOnCheckedChangeListener(this);
			
			
			mViewPager.setOnPageChangeListener(new MyPagerOnPageChangeListener());
		}

		private void iniController() {
			// TODO Auto-generated method stub
			mRadioGroup = (RadioGroup)findViewById(R.id.radioGroup);
			mRadioButton1 = (RadioButton)findViewById(R.id.btn1);
			mRadioButton2 = (RadioButton)findViewById(R.id.btn2);
			mRadioButton3 = (RadioButton)findViewById(R.id.btn3);

			

			
			mHorizontalScrollView = (HorizontalScrollView)findViewById(R.id.horizontalScrollView);
			
			mViewPager = (ViewPager)findViewById(R.id.pager);
		}

		/**
		 * ViewPager的适配器
		 * @author zj
		 * 2012-5-24 下午2:26:57
		 */
		private class MyPagerAdapter extends PagerAdapter{

			@Override
			public void destroyItem(View v, int position, Object obj) {
				// TODO Auto-generated method stub
				((ViewPager)v).removeView(mViews.get(position));
			}

			@Override
			public void finishUpdate(View arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return mViews.size();
			}

			@Override
			public Object instantiateItem(View v, int position) {
				((ViewPager)v).addView(mViews.get(position));
				return mViews.get(position);
			}

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				// TODO Auto-generated method stub
				return arg0 == arg1;
			}

			@Override
			public void restoreState(Parcelable arg0, ClassLoader arg1) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public Parcelable saveState() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public void startUpdate(View arg0) {
				// TODO Auto-generated method stub
				
			}
			
		}
		/**
		 * ViewPager的PageChangeListener(页面改变的监听器)
		 * @author zj
		 * 2012-5-24 下午3:14:27
		 */
		private class MyPagerOnPageChangeListener implements OnPageChangeListener{

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			/**
			 * 滑动ViewPager的时候,让上方的HorizontalScrollView自动切换
			 */
			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				//Log.i("zj", "position="+position);
				
				if (position == 0) {
					mViewPager.setCurrentItem(1);
				}else if (position == 1) {
					mRadioButton1.performClick();
				}else if (position == 2) {
					mRadioButton2.performClick();
				}else if (position == 3) {
					mRadioButton3.performClick();
				}else if (position == 4) {
					mViewPager.setCurrentItem(3);
				}
			}
			
		}

	public void myHandler(View target) {
		switch (target.getId()) {
		case R.id.item_cheese: showDisplay("item_cheese");break;
		case R.id.item_aegis: showDisplay("item_aegis");break;
		case R.id.item_belt_of_strength: showDisplay("item_belt_of_strength");break;
		case R.id.item_blades_of_attack: showDisplay("item_blades_of_attack");break;
		case R.id.item_blade_of_alacrity: showDisplay("item_blade_of_alacrity");break;
		case R.id.item_blink: showDisplay("item_blink");break;
		case R.id.item_boots: showDisplay("item_boots");break;
		case R.id.item_boots_of_elves: showDisplay("item_boots_of_elves");break;
		case R.id.item_bottle: showDisplay("item_bottle");break;
		case R.id.item_branches: showDisplay("item_branches");break;
		case R.id.item_broadsword: showDisplay("item_broadsword");break;
		case R.id.item_chainmail: showDisplay("item_chainmail");break;
		case R.id.item_circlet: showDisplay("item_circlet");break;
		case R.id.item_clarity: showDisplay("item_clarity");break;
		case R.id.item_claymore: showDisplay("item_claymore");break;
		case R.id.item_cloak: showDisplay("item_cloak");break;
		case R.id.item_courier: showDisplay("item_courier");break;
		case R.id.item_dust: showDisplay("item_dust");break;
		case R.id.item_flask: showDisplay("item_flask");break;
		case R.id.item_flying_courier: showDisplay("item_flying_courier");break;
		case R.id.item_gauntlets: showDisplay("item_gauntlets");break;
		case R.id.item_gem: showDisplay("item_gem");break;
		case R.id.item_ghost: showDisplay("item_ghost");break;
		case R.id.item_gloves: showDisplay("item_gloves");break;
		case R.id.item_helm_of_iron_will: showDisplay("item_helm_of_iron_will");break;
		case R.id.item_javelin: showDisplay("item_javelin");break;
		case R.id.item_lifesteal: showDisplay("item_lifesteal");break;
		case R.id.item_magic_stick: showDisplay("item_magic_stick");break;
		case R.id.item_mantle: showDisplay("item_mantle");break;
		case R.id.item_mithril_hammer: showDisplay("item_mithril_hammer");break;
		case R.id.item_ogre_axe: showDisplay("item_ogre_axe");break;
		case R.id.item_platemail: showDisplay("item_platemail");break;
		case R.id.item_quarterstaff: showDisplay("item_quarterstaff");break;
		case R.id.item_quelling_blade: showDisplay("item_quelling_blade");break;
		case R.id.item_ring_of_protection: showDisplay("item_ring_of_protection");break;
		case R.id.item_ring_of_regen: showDisplay("item_ring_of_regen");break;
		case R.id.item_robe: showDisplay("item_robe");break;
		case R.id.item_shadow_amulet: showDisplay("item_shadow_amulet");break;
		case R.id.item_slippers: showDisplay("item_slippers");break;
		case R.id.item_smoke_of_deceit: showDisplay("item_smoke_of_deceit");break;
		case R.id.item_sobi_mask: showDisplay("item_sobi_mask");break;
		case R.id.item_staff_of_wizardry: showDisplay("item_staff_of_wizardry");break;
		case R.id.item_stout_shield: showDisplay("item_stout_shield");break;
		case R.id.item_talisman_of_evasion: showDisplay("item_talisman_of_evasion");break;
		case R.id.item_tango: showDisplay("item_tango");break;
		case R.id.item_tpscroll: showDisplay("item_tpscroll");break;
		case R.id.item_ultimate_orb: showDisplay("item_ultimate_orb");break;
		case R.id.item_ward_observer: showDisplay("item_ward_observer");break;
		case R.id.item_ward_sentry: showDisplay("item_ward_sentry");break;
		case R.id.item_abyssal_blade: showDisplay("item_abyssal_blade");break;
		case R.id.item_ancient_janggo: showDisplay("item_ancient_janggo");break;
		case R.id.item_arcane_boots: showDisplay("item_arcane_boots");break;
		case R.id.item_armlet: showDisplay("item_armlet");break;
		case R.id.item_assault: showDisplay("item_assault");break;
		case R.id.item_basher: showDisplay("item_basher");break;
		case R.id.item_bfury: showDisplay("item_bfury");break;
		case R.id.item_black_king_bar: showDisplay("item_black_king_bar");break;
		case R.id.item_blade_mail: showDisplay("item_blade_mail");break;
		case R.id.item_bloodstone: showDisplay("item_bloodstone");break;
		case R.id.item_bracer: showDisplay("item_bracer");break;
		case R.id.item_buckler: showDisplay("item_buckler");break;
		case R.id.item_butterfly: showDisplay("item_butterfly");break;
		case R.id.item_cyclone: showDisplay("item_cyclone");break;
		case R.id.item_dagon: showDisplay("item_dagon");break;
		case R.id.item_desolator: showDisplay("item_desolator");break;
		case R.id.item_diffusal_blade: showDisplay("item_diffusal_blade");break;
		case R.id.item_ethereal_blade: showDisplay("item_ethereal_blade");break;
		case R.id.item_force_staff: showDisplay("item_force_staff");break;
		case R.id.item_greater_crit: showDisplay("item_greater_crit");break;
		case R.id.item_hand_of_midas: showDisplay("item_hand_of_midas");break;
		case R.id.item_headdress: showDisplay("item_headdress");break;
		case R.id.item_heart: showDisplay("item_heart");break;
		case R.id.item_heavens_halberd: showDisplay("item_heavens_halberd");break;
		case R.id.item_helm_of_the_dominator: showDisplay("item_helm_of_the_dominator");break;
		case R.id.item_hood_of_defiance: showDisplay("item_hood_of_defiance");break;
		case R.id.item_invis_sword: showDisplay("item_invis_sword");break;
		case R.id.item_lesser_crit: showDisplay("item_lesser_crit");break;
		case R.id.item_maelstrom: showDisplay("item_maelstrom");break;
		case R.id.item_magic_wand: showDisplay("item_magic_wand");break;
		case R.id.item_manta: showDisplay("item_manta");break;
		case R.id.item_mask_of_madness: showDisplay("item_mask_of_madness");break;
		case R.id.item_medallion_of_courage: showDisplay("item_medallion_of_courage");break;
		case R.id.item_mekansm: showDisplay("item_mekansm");break;
		case R.id.item_mjollnir: showDisplay("item_mjollnir");break;
		case R.id.item_monkey_king_bar: showDisplay("item_monkey_king_bar");break;
		case R.id.item_necronomicon: showDisplay("item_necronomicon");break;
		case R.id.item_null_talisman: showDisplay("item_null_talisman");break;
		case R.id.item_oblivion_staff: showDisplay("item_oblivion_staff");break;
		case R.id.item_orchid: showDisplay("item_orchid");break;
		case R.id.item_pers: showDisplay("item_pers");break;
		case R.id.item_phase_boots: showDisplay("item_phase_boots");break;
		case R.id.item_pipe: showDisplay("item_pipe");break;
		case R.id.item_poor_mans_shield: showDisplay("item_poor_mans_shield");break;
		case R.id.item_power_treads: showDisplay("item_power_treads");break;
		case R.id.item_radiance: showDisplay("item_radiance");break;
		case R.id.item_rapier: showDisplay("item_rapier");break;
		case R.id.item_refresher: showDisplay("item_refresher");break;
		case R.id.item_ring_of_aquila: showDisplay("item_ring_of_aquila");break;
		case R.id.item_ring_of_basilius: showDisplay("item_ring_of_basilius");break;
		case R.id.item_rod_of_atos: showDisplay("item_rod_of_atos");break;
		case R.id.item_sange: showDisplay("item_sange");break;
		case R.id.item_sange_and_yasha: showDisplay("item_sange_and_yasha");break;
		case R.id.item_satanic: showDisplay("item_satanic");break;
		case R.id.item_sheepstick: showDisplay("item_sheepstick");break;
		case R.id.item_shivas_guard: showDisplay("item_shivas_guard");break;
		case R.id.item_skadi: showDisplay("item_skadi");break;
		case R.id.item_soul_booster: showDisplay("item_soul_booster");break;
		case R.id.item_soul_ring: showDisplay("item_soul_ring");break;
		case R.id.item_sphere: showDisplay("item_sphere");break;
		case R.id.item_tranquil_boots: showDisplay("item_tranquil_boots");break;
		case R.id.item_travel_boots: showDisplay("item_travel_boots");break;
		case R.id.item_ultimate_scepter: showDisplay("item_ultimate_scepter");break;
		case R.id.item_urn_of_shadows: showDisplay("item_urn_of_shadows");break;
		case R.id.item_vanguard: showDisplay("item_vanguard");break;
		case R.id.item_veil_of_discord: showDisplay("item_veil_of_discord");break;
		case R.id.item_vladmir: showDisplay("item_vladmir");break;
		case R.id.item_void_stone: showDisplay("item_void_stone");break;
		case R.id.item_wraith_band: showDisplay("item_wraith_band");break;
		case R.id.item_yasha: showDisplay("item_yasha");break;
		case R.id.item_demon_edge: showDisplay("item_demon_edge");break;
		case R.id.item_eagle: showDisplay("item_eagle");break;
		case R.id.item_energy_booster: showDisplay("item_energy_booster");break;
		case R.id.item_hyperstone: showDisplay("item_hyperstone");break;
		case R.id.item_mystic_staff: showDisplay("item_mystic_staff");break;
		case R.id.item_orb_of_venom: showDisplay("item_orb_of_venom");break;
		case R.id.item_point_booster: showDisplay("item_point_booster");break;
		case R.id.item_reaver: showDisplay("item_reaver");break;
		case R.id.item_relic: showDisplay("item_relic");break;
		case R.id.item_ring_of_health: showDisplay("item_ring_of_health");break;
		case R.id.item_vitality_booster: showDisplay("item_vitality_booster");break;



		}
		
		
	}
	

	
	public void showDisplay(String item){
		
		final Dota2EquipmentDialog dialog = new Dota2EquipmentDialog(Dota2EquipmentActivity.this, R.style.MyDialog);
		//	dialog.setProperty(400, 600);
		     dialog.setContentView(R.layout.dota2_equi_dialog);
		     DotaEquipment equipment =new DotaEquipment();
		     
	          
		     
				dialog. setPositiveButton("    ", new android.view.View.OnClickListener() {
					public void onClick(View v) {
						dialog.dismiss();
					}
				    });
	   		    InputStream fis;
				try {
					fis = getAssets().open( "dota2/"+item +".jpg");
					Bitmap bm = BitmapFactory.decodeStream(fis);
					dialog.setItemImage(bm);
					String name = equipment.getClass().getField(item+"_name").get(equipment).toString();
					String price = equipment.getClass().getField(item+"_price").get(equipment).toString();
					String content = equipment.getClass().getField(item+"_property").get(equipment).toString();
					dialog.setName(Html.fromHtml(name));
					dialog.setPrice(Html.fromHtml(price));
					dialog.setContent(Html.fromHtml(content));
					
					String[] childnames = (String[])equipment.getClass().getField(item+"_need").get(equipment);
					if(childnames!=null && childnames.length>0){
						 if(childnames.length>=1){
							 String childname = childnames[0];
							 InputStream fis1;
				    			try {
				    				fis1 = getAssets().open( "dota2/"+childname +".jpg");
				    				Bitmap bm1 = BitmapFactory.decodeStream(fis1);
									dialog.setItem_childimage1(bm1);
				    			} catch (IOException e) {
				    				e.printStackTrace();
				    			}
				    			String childImageName= equipment.getClass().getField(childname+"_name").get(equipment).toString();
								dialog.setChildimage1name(Html.fromHtml(childImageName));
								String childprice = equipment.getClass().getField(childname+"_price").get(equipment).toString();
								dialog.setChildimage1price(Html.fromHtml(childprice));
						 }
						 if(childnames.length>=2){
							 String childname = childnames[1].toString();
							 InputStream fis1;
				    			try {
				    				fis1 = getAssets().open( "dota2/"+childname +".jpg");
				    				Bitmap bm1 = BitmapFactory.decodeStream(fis1);
									dialog.setItem_childimage2(bm1);
				    			} catch (IOException e) {
				    				e.printStackTrace();
				    			}
				    			String childImageName= equipment.getClass().getField(childname+"_name").get(equipment).toString();
								dialog.setChildimage2name(Html.fromHtml(childImageName));
								String childprice = equipment.getClass().getField(childname+"_price").get(equipment).toString();
								dialog.setChildimage2price(Html.fromHtml(childprice));
						 }
						 if(childnames.length>=3){
								String childname = childnames[2].toString();
							 InputStream fis1;
				    			try {
				    				fis1 = getAssets().open( "dota2/"+childname+".jpg");
				    				Bitmap bm1 = BitmapFactory.decodeStream(fis1);
									dialog.setItem_childimage3(bm1);
				    			} catch (IOException e) {
				    				e.printStackTrace();
				    			}
				    			String childImageName= equipment.getClass().getField(childname+"_name").get(equipment).toString();
								dialog.setChildimage3name(Html.fromHtml(childImageName));
								String childprice = equipment.getClass().getField(childname+"_price").get(equipment).toString();
								dialog.setChildimage3price(Html.fromHtml(childprice));
						 }
						 if(childnames.length>=4){
								String childname = childnames[3].toString();
							 InputStream fis1;
				    			try {
				    				fis1 = getAssets().open( "dota2/"+childname+".jpg");
				    				Bitmap bm1 = BitmapFactory.decodeStream(fis1);
									dialog.setItem_childimage3(bm1);
				    			} catch (IOException e) {
				    				e.printStackTrace();
				    			}
				    			String childImageName= equipment.getClass().getField(childname+"_name").get(equipment).toString();
								dialog.setChildimage3name(Html.fromHtml(childImageName));
								String childprice = equipment.getClass().getField(childname+"_price").get(equipment).toString();
								dialog.setChildimage3price(Html.fromHtml(childprice));
						 }
						 Object itemReel =equipment.getClass().getField(item+"_reel").get(equipment);
						 if(itemReel !=null){
							 dialog.setReel(Html.fromHtml("卷轴： <font color='#DB8931'>"+itemReel+"</font>"));
						 }
						
						
					}else{
					
						
					}
					
	
				} catch (Exception e) {
					e.printStackTrace();
				}
				
		     dialog.show();
	}


}
